/**
 * 作者: lby
 * 日期: 2024-11-20 09:59:27
 * 备注: 用户不婚主义子嗣表
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserChildAlone;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class UserChildAloneModel extends BaseModel implements Model {
	public static final String LISTKEY = "user_id";
	public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
	public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
	public static final boolean ORDER_LIST = true;// 是否需要额外的有序列表

	protected long id; // id
	protected long userId; // 玩家id
	protected String name = ""; // 名字
	protected int quality; // 品质
	protected int career; // 职业
	protected int partner; // 父亲
	protected int secretary; // 母亲
	protected int appearance; // 外观 (0表示长相随父亲，取partner字段去partner_main表找头（其他值就根据secretary去secretary_main表找头），身体根据career去child_career_appearance表找)
	protected byte sex; // 性别 (0女1男)
	protected long earnAdd; // 毕业时总的城市收益加成

	public void reset() {
	}

	protected String getKey() {
		return "UserChildAlone." + id;
	}

	protected String getListKey() {
		return "UserChildAlone." + userId;
	}

	protected String getListItemKey() {
		return id + "";
	}

	public void doCreate() {
		setTableId();
		insert();
	}

	public void setTableId() {
		putId(incrTableId("user_child_alone"));
	}

	public JsonObject insert() {
		return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
	}

	public void delete() {
		super.delete(getKey(), id, UPDATE_DB_TYPE, userId, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
	}

	/**
	 * 根据入库类型更新
	 */
	public JsonObject update() {
		return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, userId, getAllObj(), LISTKEY, getListKey(), getListItemKey());
	}

	/**
	 * 实时入队列
	 */
	public JsonObject updateQueueNow() {
		return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
	}

	public JsonObject getAllObj(){
		JsonObject obj = new JsonObject();
		obj.put("id", id); // id
		obj.put("userId", userId); // 玩家id
		obj.put("name", name); // 名字
		obj.put("quality", quality); // 品质
		obj.put("career", career); // 职业
		obj.put("partner", partner); // 父亲
		obj.put("secretary", secretary); // 母亲
		obj.put("appearance", appearance); // 外观 (0表示长相随父亲，取partner字段去partner_main表找头（其他值就根据secretary去secretary_main表找头），身体根据career去child_career_appearance表找)
		obj.put("sex", sex); // 性别 (0女1男)
		obj.put("earnAdd", earnAdd); // 毕业时总的城市收益加成
		return obj;
	}

	public PBUserChildAlone.Builder takeInitBuilder() {
		PBUserChildAlone.Builder sb = PBUserChildAlone.newBuilder();
		if (!name.isEmpty()) {
			sb.setName(name);
		}
		if (quality != 0) {
			sb.setQuality(quality);
		}
		if (career != 0) {
			sb.setCareer(career);
		}
		if (partner != 0) {
			sb.setPartner(partner);
		}
		if (secretary != 0) {
			sb.setSecretary(secretary);
		}
		if (appearance != 0) {
			sb.setAppearance(appearance);
		}
		if (sex != 0) {
			sb.setSex(sex);
		}
		if (earnAdd != 0) {
			sb.setEarnAdd(earnAdd);
		}
		return sb;
	}

	public PBUserChildAlone.Builder takeBuilder() {
		if (updateObj == null || updateObj.isEmpty()) {
			return takeInitBuilder();
		}
		PBUserChildAlone.Builder sb = PBUserChildAlone.newBuilder();
		if (isFieldUpdate("name")) {
			sb.setName(name);
		}
		if (isFieldUpdate("quality")) {
			sb.setQuality(quality);
		}
		if (isFieldUpdate("career")) {
			sb.setCareer(career);
		}
		if (isFieldUpdate("partner")) {
			sb.setPartner(partner);
		}
		if (isFieldUpdate("secretary")) {
			sb.setSecretary(secretary);
		}
		if (isFieldUpdate("appearance")) {
			sb.setAppearance(appearance);
		}
		if (isFieldUpdate("sex")) {
			sb.setSex(sex);
		}
		if (isFieldUpdate("earnAdd")) {
			sb.setEarnAdd(earnAdd);
		}
		return sb;
	}

	public long getId() {
		return id;
	}

	public void putId(long id) {
		if (this.id != id) {
			this.id = id;
			updateRedisHashField(getKey(), "id", this.id);
		}
	}

	public void addId(long value) {
		this.id += value;
		updateRedisHashField(getKey(), "id", this.id);
	}

	public long getUserId() {
		return userId;
	}

	public void putUserId(long userId) {
		if (this.userId != userId) {
			this.userId = userId;
			updateRedisHashField(getKey(), "userId", this.userId);
		}
	}

	public void addUserId(long value) {
		this.userId += value;
		updateRedisHashField(getKey(), "userId", this.userId);
	}

	public String getName() {
		return name;
	}

	public void putName(String name) {
		if (!this.name.equals(name)) {
			this.name = name;
			updateRedisHashField(getKey(), "name", this.name);
		}
	}

	public int getQuality() {
		return quality;
	}

	public void putQuality(int quality) {
		if (this.quality != quality) {
			this.quality = quality;
			updateRedisHashField(getKey(), "quality", this.quality);
		}
	}

	public void addQuality(int value) {
		this.quality += value;
		updateRedisHashField(getKey(), "quality", this.quality);
	}

	public int getCareer() {
		return career;
	}

	public void putCareer(int career) {
		if (this.career != career) {
			this.career = career;
			updateRedisHashField(getKey(), "career", this.career);
		}
	}

	public void addCareer(int value) {
		this.career += value;
		updateRedisHashField(getKey(), "career", this.career);
	}

	public int getPartner() {
		return partner;
	}

	public void putPartner(int partner) {
		if (this.partner != partner) {
			this.partner = partner;
			updateRedisHashField(getKey(), "partner", this.partner);
		}
	}

	public void addPartner(int value) {
		this.partner += value;
		updateRedisHashField(getKey(), "partner", this.partner);
	}

	public int getSecretary() {
		return secretary;
	}

	public void putSecretary(int secretary) {
		if (this.secretary != secretary) {
			this.secretary = secretary;
			updateRedisHashField(getKey(), "secretary", this.secretary);
		}
	}

	public void addSecretary(int value) {
		this.secretary += value;
		updateRedisHashField(getKey(), "secretary", this.secretary);
	}

	public int getAppearance() {
		return appearance;
	}

	public void putAppearance(int appearance) {
		if (this.appearance != appearance) {
			this.appearance = appearance;
			updateRedisHashField(getKey(), "appearance", this.appearance);
		}
	}

	public void addAppearance(int value) {
		this.appearance += value;
		updateRedisHashField(getKey(), "appearance", this.appearance);
	}

	public byte getSex() {
		return sex;
	}

	public void putSex(byte sex) {
		if (this.sex != sex) {
			this.sex = sex;
			updateRedisHashField(getKey(), "sex", this.sex);
		}
	}

	public void addSex(byte value) {
		this.sex += value;
		updateRedisHashField(getKey(), "sex", this.sex);
	}

	public long getEarnAdd() {
		return earnAdd;
	}

	public void putEarnAdd(long earnAdd) {
		if (this.earnAdd != earnAdd) {
			this.earnAdd = earnAdd;
			updateRedisHashField(getKey(), "earnAdd", this.earnAdd);
		}
	}

	public void addEarnAdd(long value) {
		this.earnAdd += value;
		updateRedisHashField(getKey(), "earnAdd", this.earnAdd);
	}

}